package com.choosefine.it.basedata.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.yaml.snakeyaml.util.UriEncoder;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.basedata.model.WorkTypeClass;
import com.choosefine.it.basedata.service.WorkTypeClassService;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.common.utils.DateUtils;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@RestController
@RequestMapping("/basedata/worktypeclass")
@Api(value = "工种父类相关", description = "顾文伟")
public class WorkTypeClassController extends BaseController {
	Logger logger = LoggerFactory.getLogger(WorkTypeClassController.class);

	@Autowired
	WorkTypeClassService worktypeClassService;

	/**
	 * 添加工种父类别
	 * 
	 * @param worktype
	 * @param response
	 * @return
	 * @throws BusinessException 
	 * @throws Exception
	 */
	@ApiOperation(value = "存储工种父类别", notes = "", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@ApiResponses({
			@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@RequestMapping(method = RequestMethod.POST)
	public String save(@RequestBody WorkTypeClass worktype) throws BusinessException {
		String result;
		if (worktype != null) {
			worktype.setCtime(DateUtils.getNowTime());
			worktype.setIsValid(CommonConstant.VALID);
			Integer num = worktypeClassService.insert(worktype);
			if (num >0) {
				result = responseSuccess(num, ResultCode.SUCCESS_CREATED,
						ResultCode.MSG_CREATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	/**
	 * 更新工种父类型数据
	 * 
	 * @param worktype
	 *            父类型名
	 * @return
	 * @throws BusinessException 
	 */
	@ApiOperation(value = "更改工种父类型", notes = "")
	@ResponseBody
	@ApiResponses({
			@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@RequestMapping(method = RequestMethod.PUT)
	public String update(@RequestBody WorkTypeClass worktype) throws BusinessException {
		String result = null;
		if (worktype != null) {
			worktype.setUtime(DateUtils.getNowTime());
			int num = worktypeClassService.update(worktype);
			if (num >0) {
				result = responseSuccess(num, ResultCode.SUCCESS_CREATED,
						ResultCode.MSG_UPDATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	/**
	 * 根据ID获取工种父类型名
	 * 
	 * @param id
	 *            工种父类型ID
	 * @return
	 */
	@ApiOperation(value = "根据ID取工种父类别", notes = "")
	@ApiImplicitParam(name = "id", value = "工种父类别ID", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String getWorkTypeById(@PathVariable Integer id) {
		String result = null;
		if (id != null) {
			WorkTypeClass worktype = worktypeClassService.findByPrimaryKey(id);
			if (worktype != null) {
				result = responseSuccess(worktype);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	/**
	 * 根据ID删除工种父类型
	 * 
	 * @param id
	 *            工种父类型ID
	 * @return
	 */
	@ApiOperation(value = "根据ID删除工种父类别", notes = "")
	@ApiImplicitParam(name = "id", value = "工种ID", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable Integer id) {
		String result = null;
		if (id != null) {
			int num = worktypeClassService.deleteByPrimaryKey(id);
			if (num>0) {
				result = responseSuccess(num);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;

	}

//	@ApiOperation(value = "获取用户列表", notes = "根据url分页的参数来查询用户的信息列表")
//	@ApiImplicitParams({
//			@ApiImplicitParam(name = "pageNum", value = "起始行", required = true, dataType = "Integer", paramType = "path"),
//			@ApiImplicitParam(name = "pageSize", value = "一页显示的行数", required = true, dataType = "Integer", paramType = "path") })
//	@RequestMapping(method = RequestMethod.GET, value = "/UserList/{pageNum}/{pageSize}", produces = "application/json")
//	public String getUserList(@PathVariable Integer pageNum,
//			@PathVariable Integer pageSize) {
//		String result = null;
//		List<User> userlist = userService.findAllByPage(pageNum,pageSize);
//
//		if (userlist != null) {
//			result = responseArraySuccess(userlist);
//		} else {
//			result = responseFail(ResultCode.MSG_ERROR);
//		}
//		return result;
//	}
	
	@ApiOperation(value = "获取所有工种父类", notes = "")
	@RequestMapping(value = "/worktypeclasslist", method = RequestMethod.GET)
	public String getWorkTypeClassList() throws BusinessException {
		String result = null;
		List<WorkTypeClass> worktypeclass = worktypeClassService.findAllByPage();
		result = responseArraySuccess(worktypeclass);
		return result;
	}

	@ApiOperation(value = "根据名称获取工种类别", notes = "")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pageNum", value = "起始页", dataType = "Integer", required = true, paramType = "query"),
		@ApiImplicitParam(name = "pageSize", value = "每页行", dataType = "Integer", required = true, paramType = "query"),
		@ApiImplicitParam(name = "typeclassname", value = "工种类别名", required = true, dataType = "String", paramType = "path")})
	@RequestMapping(value = "/worktypeclasswithname/{typeclassname}", method = RequestMethod.GET)
	public String getWorkTypeWithName(@PathVariable String typeclassname, Integer pageSize, Integer pageNum) {
		String result = null;
		PageInfo<WorkTypeClass> worktypeclass = worktypeClassService.selectWorkTypeClassListByName(UriEncoder.decode(typeclassname),pageSize,pageNum);
		if (worktypeclass != null) {
			result = responseArraySuccess(worktypeclass);
		} else {
			result = responseFail(ResultCode.MSG_DATA_EMPTY);
		}
		return result;
	}
	@ApiOperation(value = "工种类别排序")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "fromId", value = "移动项", dataType = "Integer", required = true, paramType = "path"),
		@ApiImplicitParam(name = "toId", value = "移动到的位置", dataType = "Integer", required = true, paramType = "path"),
		@ApiImplicitParam(name = "order", value = "移动标识（1上移-1下移）", dataType = "Integer", required = true, paramType = "path")
	})
	@RequestMapping(value = "/workTypeClassOrder/{fromId}/{toId}/{order}", method = RequestMethod.PUT)
	public String teamTypeOrder(@PathVariable Integer fromId, @PathVariable Integer toId, @PathVariable Integer order){
		String result = null;
		int num = 0;
		if(order>0){
			num += worktypeClassService.workTypeClassOrderUp(fromId, toId);
		}else{
			num += worktypeClassService.workTypeClassOrderDown(fromId, toId);
		}
		if(num>0){
			result = responseSuccess(num);
		}else{
			result = responseFail();
		}
		return result;
	}
}
